package god.dream.loginfo.controller;

import god.dream.loginfo.model.LogInfo;
import god.dream.loginfo.service.LogInfoService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class LogInfoNotFormController extends MultiActionController {
	@Autowired
	private LogInfoService logInfoService;
	
	public ModelAndView checkLoginId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String loginId = request.getParameter("loginId");
		LogInfo logInfo = new LogInfo();
		if(!loginId.isEmpty())
			logInfo = logInfoService.findLogInfoWithLoginId(loginId);
		ModelAndView mav = new ModelAndView("check_loginid_form_view");
		mav.addObject("logInfo", logInfo);
		mav.addObject("loginId", loginId);

		return mav;
	}
	
	public ModelAndView listLogInfos(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<LogInfo> logInfoList = logInfoService.findLogInfoList();
		ModelAndView mav = new ModelAndView("list_loginfos");
		mav.addObject("loginfos", logInfoList);

		return mav;
	}
	public ModelAndView deleteUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		if(id != null)
			logInfoService.removeLogInfo(Integer.parseInt(id));
		ModelAndView mav = new ModelAndView("redirect:/cont/listLogInfo.htm");

		return mav;
	}
	public ModelAndView selectUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		LogInfo logInfo = new LogInfo();
		if(id != null)
			logInfo = logInfoService.findLogInfo(Integer.parseInt(id));
		ModelAndView mav = new ModelAndView("select_loginfo");
		mav.addObject("loginfo", logInfo);

		return mav;
	}

	public LogInfoService getLogInfoService() {
		return logInfoService;
	}

	public void setLogInfoService(LogInfoService logInfoService) {
		this.logInfoService = logInfoService;
	}
}
